using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace PlainConcepts.Fx.Dnie.SmartCard.Interop
{
	/// <summary>
	/// "Managed Smart Card" PInvoke helper interface.  This exposes 
	/// SCardUIDlgSelectCardW from PC/SC, as well as the Card Module API, via
	/// an additional native code helper dll.
	/// 
	/// Each function roughly corresponds to a Win32 API documented with 
	/// the Platform SDK (just remove MgSc to get the name of the wrapped
	/// function).
	/// </summary>
    class MgSc
	{
		[DllImport("PlainConcepts.SmartCard.dll", CharSet = CharSet.Unicode)]
		public static extern Int32 MgScSCardUIDlgSelectCardW(
			[MarshalAs(UnmanagedType.LPStruct)] [In, Out] 
                PCSCOpenCardNameWEx ocnwex);

		[DllImport("PlainConcepts.SmartCard.dll", CharSet = CharSet.Unicode)]
		public static extern UInt32 MgScCardAcquireContext(
			[MarshalAs(UnmanagedType.LPStruct)] [In, Out] 
                MgScContext mscc,
			IntPtr pcscContext,
			IntPtr cardHandle,
			[MarshalAs(UnmanagedType.LPWStr)] string cardName,
			[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] atr,
			UInt32 atrLength,
			UInt32 flags);

		[DllImport("PlainConcepts.SmartCard.dll", CharSet = CharSet.Unicode)]
		public static extern UInt32 MgScCardAuthenticatePin(
			[MarshalAs(UnmanagedType.LPStruct)] [In, Out] 
                MgScContext mscc,
			[MarshalAs(UnmanagedType.LPWStr)] string userId,
			[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pin,
			UInt32 pinLength,
			ref UInt32 attemptsRemaining);

		[DllImport("PlainConcepts.SmartCard.dll", CharSet = CharSet.Ansi)]
		public static extern UInt32 MgScCardCreateFile(
			[MarshalAs(UnmanagedType.LPStruct)] [In, Out] 
                MgScContext mscc,
			[MarshalAs(UnmanagedType.LPStr)] string directoryName,
			[MarshalAs(UnmanagedType.LPStr)] string fileName,
			UInt32 initialCreationSize,
			UInt32 accessCondition);

		[DllImport("PlainConcepts.SmartCard.dll", CharSet = CharSet.Unicode)]
		public static extern UInt32 MgScCardDeauthenticate(
			[MarshalAs(UnmanagedType.LPStruct)] [In, Out] 
                MgScContext mscc,
			[MarshalAs(UnmanagedType.LPWStr)] string userId,
			UInt32 flags);

		[DllImport("PlainConcepts.SmartCard.dll", CharSet = CharSet.Unicode)]
		public static extern UInt32 MgScCardDeleteContext(
			[MarshalAs(UnmanagedType.LPStruct)] [In, Out] 
                MgScContext mscc);

		[DllImport("PlainConcepts.SmartCard.dll", CharSet = CharSet.Ansi)]
		public static extern UInt32 MgScCardDeleteFile(
			[MarshalAs(UnmanagedType.LPStruct)] [In, Out] 
                MgScContext mscc,
			[MarshalAs(UnmanagedType.LPStr)] string directoryName,
			[MarshalAs(UnmanagedType.LPStr)] string fileName,
			UInt32 flags);

		[DllImport("PlainConcepts.SmartCard.dll", CharSet = CharSet.Ansi)]
		public static extern UInt32 MgScCardReadFile(
			[MarshalAs(UnmanagedType.LPStruct)] [In, Out] 
                MgScContext mscc,
			[MarshalAs(UnmanagedType.LPStr)] string directoryName,
			[MarshalAs(UnmanagedType.LPStr)] string fileName,
			UInt32 flags,
			[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 5)] [In, Out]
                byte[] data,
			  ref UInt32 dataLength);

		[DllImport("PlainConcepts.SmartCard.dll", CharSet = CharSet.Ansi)]
		public static extern UInt32 MgScCardWriteFile(
			[MarshalAs(UnmanagedType.LPStruct)] [In, Out] 
                MgScContext mscc,
			[MarshalAs(UnmanagedType.LPStr)] string directoryName,
			[MarshalAs(UnmanagedType.LPStr)] string fileName,
			UInt32 flags,
			[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 5)] 
                byte[] data,
			UInt32 dataLength);
	}
}
